import Axios from 'axios'
import qs from 'qs'
import common from '@/common'
// 添加返回拦截器

Axios.interceptors.response.use((res) => {
  return res
}, (error) => {
  if (error.response) {
    // The request was made and the server responded with a status code
    // that falls out of the range of 2xx
    console.log(error.response.data)
    console.log(error.response.status)
    console.log(error.response.headers)
    if (error.response.status === 401 && error.response.data.error === 'invalid_token') {
      common.clearToken()
      window.location.pathname = '/login'
      console.log('error.response.headers')
      return
    }
  } else if (error.request) {
    // The request was made but no response was received
    // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
    // http.ClientRequest in node.js
    console.log(error.request)
  } else {
    // Something happened in setting up the request that triggered an Error
    console.log('Error', error.message)
  }
  console.log(error.config)
  return Promise.reject(error)
})

// 添加请求拦截器
Axios.interceptors.request.use(function (config) {
  /* FormData 不包括File，则转为json */
  if (config.data instanceof FormData) {
    let includeFile = false
    config.data.forEach((value, key) => {
      if (value instanceof File) {
        includeFile = true
      }
    })
    if (!includeFile) {
      const data = {}
      config.data.forEach((value, key) => {
        data[key] = key === 'requiresAuth' ? value === '1' : value
      })
      config.data = data
    }
  }

  /* 判断是否要授权 */
  if (config.data instanceof FormData) {
    if (config.data.has('requiresAuth')) {
      if (config.data.get('requiresAuth') === '1') {
        let token = common.getToken()
        if (!token) {
          window.location.pathname = '/login'
          return config
        }
        config.headers['Authorization'] = `bearer ${token}`
      }
      config.data.delete('requiresAuth')
    }
  } else {
    if (config.data.requiresAuth) {
      let token = common.getToken()
      if (!token) {
        window.location.pathname = '/login'
        return config
      }
      config.data.access_token = token
    }
    delete config.data.requiresAuth
  }

  if (config.method === 'get') {
    if (Object.keys(config.data).length > 0) {
      if (config.url.includes('?')) {
        config.url += qs.stringify(config.data)
      } else {
        config.url += '?' + qs.stringify(config.data)
      }
    }
    config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
  //  console.log(config)
  } else {
    if (config.data instanceof FormData) {
      config.headers['Content-Type'] = 'multipart/form-data'
    } else {
      config.data = qs.stringify(config.data)
      config.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    }
  }
  // config.headers['Content-Type'] = 'application/json;charset=UTF-8'
  return config
}, (error) => {
  return Promise.reject(error)
})

class Interceptors {
  init () {

  }
}

export default new Interceptors()
